package org.haredot.utils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 工具类
 */
public class WebUtils {

    private static ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<>();

    private static ThreadLocal<HttpServletResponse> responseLocal = new ThreadLocal<>();

    public static void setRequest(HttpServletRequest request) {
        requestLocal.set(request);
    }

    public static void setResponse(HttpServletResponse response) {
        responseLocal.set(response);
    }

    public static void setRequestAndResponse(HttpServletRequest request, HttpServletResponse response) {
        requestLocal.set(request);
        responseLocal.set(response);
    }

    public static HttpServletRequest getRequest() {
        return requestLocal.get();
    }

    public static HttpServletResponse getResponse() {
        return responseLocal.get();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static ServletContext getApplication() {
        return getRequest().getSession().getServletContext();
    }


    public static void removeRequest() {
        requestLocal.remove();
    }

    public static void removeResponse() {
        responseLocal.remove();
    }

    public static void removeRequestAndResponse() {
        requestLocal.remove();
        responseLocal.remove();
    }
}
